package com.caogl.zxing_gen_qrcode_barcode_demo;

//import com.caogl.zxing_gen_qrcode_barcode_demo.drop.MatrixToImageWriter;
//import com.caogl.zxing_gen_qrcode_barcode_demo.drop.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;

/**
 * @see http://stackoverflow.com/questions/2658663
 * 在图片上输出文字
 */
public class GenBufferedImageWithBarCodeQRcodeDemo {

    public static BufferedImage gen_qrcode() throws WriterException {

        String content = "120605181003;http://www.cnblogs.com/jtmjx";
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        java.util.Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 110, 110,hints);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

    public static BufferedImage gen_barcode(String content) throws WriterException {

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        java.util.Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.CODE_128, 100, 18,hints);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }


//    private BufferedImage image;




     BufferedImage gen_image() throws WriterException {
        BufferedImage qrcode=gen_qrcode();
        BufferedImage barcode1=gen_barcode("a1111111111");
        BufferedImage barcode2=gen_barcode("b1111111111");


        BufferedImage img = new BufferedImage(
                603, 240, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = img.createGraphics();

        g.drawImage(qrcode, 294, 10, null);

        g.drawImage(barcode1, 294, 126, null);
        g.drawImage(barcode2, 294, 171, null);
        g.setPaint(Color.black);

        g.setFont(new Font("微软雅黑", Font.PLAIN, 8));
        String s1 = "a1111111111";
        g.drawString(s1, 294, 126 + 18 + g.getFontMetrics().getHeight());

        String s2 = "b1111111111";
        g.drawString(s2, 294, 171+18+g.getFontMetrics().getHeight());

        g.setFont(new Font("微软雅黑", Font.PLAIN, 10));
        String s3 = "注意不要撕毁条码";
        g.drawString(s3, 454, 38+g.getFontMetrics().getHeight());

        String s4 = "这是测试哦啊嗯也！哈哈嗯嗯";
        g.drawString(s4, 454, 80+g.getFontMetrics().getHeight());

        g.dispose();
//        g2d.
        return img;
    }

    //测试方法
    public void test_gen_image_write_to_file() throws Exception {

        BufferedImage image = gen_image();
        File outputfile = new File("d:/tmp/image.png");
        ImageIO.write(image, "png", outputfile);
    }

    public static void main(String[] args) throws Exception {
        new GenBufferedImageWithBarCodeQRcodeDemo().test_gen_image_write_to_file();
    }
}
